package com.example.swordjava;


import java.util.ArrayList;
import java.util.Stack;

public class Test6 {

    public static void main(String[] args) {

        int[] a = {2, 3, 1, 0, 2, 5, 3};

    }

    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    /**
     * 输入一个链表的头节点，按链表从尾到头的顺序返回每个节点的值（用数组返回）。
     */

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

            ArrayList<Integer> ans = new ArrayList<>();

            Stack<Integer> stack = new Stack<>();
            while (listNode != null) {
                stack.push(listNode.val);
                listNode = listNode.next;
            }
            while (!stack.isEmpty()) {
                ans.add(stack.pop());
            }
            return ans;
    }

}
